home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-02
/
pas_0593.zip
/
DLTLINES.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1993-05-30
|
5KB
|
123 lines
{─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
Msg : 193 of 773
From : Wayne Moses 1:255/6.0 02 May 93 16:59
To : Ryan Matijcio
Subj : Deleting Lines
────────────────────────────────────────────────────────────────────────────────
*> Ryan Matijcio was heard mumbling to Wayne Moses on 01 May 93 11:26:48
*> Regarding : Deleting Lines
WM> I see ... well you'll have to open the source file to read from, open a
WM> temp file to write to, then on a line-by-line basis read(file1) and
WM> write(file2) with the exception of the first five lines. Do this until
WM> eof(file1), close both files, then copy the temp file onto file1.
RM> Could you give me a sample of this code? As I really don't
RM> know anything regarding file manipulation.
Okay, Ryan ... here's some code for you. It ran well on my machine.
-------- [ CUT LINE ]----------- 8< --------}
Program DLTLINES;
{==========================================================================
Sample code for a program to delete "n" lines from a text file which
contains at least "n" lines. This code is in response to a query from
Ryan Matijcio.
Written by : Wayne Moses
Written in : Turbo Pascal 6.0
Distribution : Public Domain
Date : May 2, 1993
Usage syntax : dltlines textfilename [n]
where : textfilename is the DOS full pathname
n is the optional number of lines to delete;
default is 5
Note : Error trapping has, for the most part, been ignored.
==========================================================================}
var
strTextFile, LineRead : string;
TextFile, Temp : text;
LinesToDelete, NumLines, code, i : integer;
begin
(* First get the command line parameters *)
if ParamCount = 0 then { No parameters passes; display syntax }
begin { quit. }
writeln;
writeln('Usage syntax : dltlines textfilename [n]');
writeln;
writeln('where : textfilename is the DOS full pathname');
writeln(' n is the optional number of lines to delete;');
writeln(' default is 5.');
halt;
end
else { Parameters were passed; no error }
begin { checking is done in this code! }
strTextFile := ParamStr(1);
if ParamCount = 1 then LinesToDelete := 5 else
val(ParamStr(2), LinesToDelete, code);
end;
(* Next, check if the file has more than LinesToDelete lines of text *)
assign(TextFile,strTextFile); { Associate the external file in first }
reset(TextFile); { parameter with TextFile; prepare it }
NumLines := 0; { for reading from ... }
while not eof(TextFile) do
begin
readln(TextFile,LineRead);
inc(NumLines);
end;
close(TextFile);
if NumLines < LinesToDelete then { In this case, print this note and }
begin { quit. }
writeln;
writeln('ERROR!! : ',strTextFile,' contains less than ',
LinesToDelete,' lines.');
halt;
end;
(* At this point, we can assume that the file has enough lines to
delete, so we begin deleting from the top. What we are really doing
is not writing all but the top LinesToDelete lines to a temp file *)
assign(TextFile,strTextFile);
reset(TextFile);
assign(Temp,'DLTLINES.TMP');
rewrite(Temp);
(* First skip the first LinesToDelete lines *)
for i := 1 to LinesToDelete do readln(TextFile, LineRead);
(* Now read and write the balance of TextFile *)
while not eof(TextFile) do
begin
readln(TextFile, LineRead);
writeln(Temp, LineRead);
end;
close(TextFile); close(Temp); { Close both files to save them }
(* We now have the modified version of TextFile in DLTLINES.TMP on the
default drive. We want now to delete TextFile and rename
DLTLINES.TMP as strTextFile *)
erase(TextFile); rename(Temp, strTextFile);
(* There you go ... its done ... crude but done. It will do the trick.
You can add to this if you like, and compile it as an .EXE file
for further use. Have fun. *)
end.